<?php

	class SelectStocks extends Connection {
		private $conn;
		public $stocks_number;



		public function __construct() {
			Connection::init();
			$this->conn = $this->getConnection();
		}
		
		public function extractFrontPageStocks(){
			$stmt = $this->conn->query("	select 		s.id,
														s.name,
														s.description,
														s.image,
														s.quantity_type,
														s.minimum_quantity,
														s.producer,
														s.price,
														s.promo,
														s.promo_price,
														c.name as category,
														s.front_page
												from 	Stock s
												join 	category c
													on 	c.id = s.category_id
												where 	s.front_page is not null
												");
			$stmt->execute();
			$stockArray = $stmt->fetchAll(PDO::FETCH_ASSOC);
			$stockArrayObj = array();
			foreach ($stockArray as $stock) {
				$temp = new Stock();
				$temp->Init($stock);
				$stockArrayObj[] = $temp;
				unset($temp);
			}
			return($stockArrayObj);
			
			
		}
		public function search($word){
			$stmt = $this->conn->prepare("	select 		s.id,
														s.name,
														s.description,
														s.image,
														s.quantity_type,
														s.minimum_quantity,
														s.producer,
														s.price,
														s.promo,
														s.promo_price,
														c.name as category,
														s.front_page
												from 	Stock s
												join 	category c
													on 	c.id = s.category_id
												where 	s.name like :word or
														s.description like :word or
														s.producer like :word or
														c.name like :word
												");
			
			$wordPattern = "%" . $word . "%";
			$stmt->bindParam(':word', $wordPattern);
			$stmt->execute();
			$stockArray = $stmt->fetchAll(PDO::FETCH_ASSOC);
			$stockArrayObj = array();
			foreach ($stockArray as $stock) {
				$temp = new Stock();
				$temp->Init($stock);
				$stockArrayObj[] = $temp;
				unset($temp);
			}
			return($stockArrayObj);
		
		}

		public function extractAllFromCategory($name,$page) {
			try {
				$begin = ($page-1)*10;
				$numbers = 10;
/*				$stmt = $this->conn->prepare("	select 	count(id) as numbers
												from 	Stock
												where 	c.name = :name");
				$stmt->bindParam(":name", $name,PDO::PARAM_STR);
				$stmt->execute();
				$this->setStocksNumber($stmt->fetchColumn());*/

				$stmt = $this->conn->prepare("	select 	s.id,
														s.name,
														s.description,
														s.image,
														s.quantity_type,
														s.minimum_quantity,
														s.producer,
														s.price,
														s.promo,
														s.promo_price,
														c.name as category,
														s.front_page
												from 	Stock s
												join 	category c
													on 	c.id = s.category_id
												where 	c.name = :name
												Limit 	:begin ,:numbers");
				$stmt->bindParam(":name", $name,PDO::PARAM_STR);
				$stmt->bindParam(":begin",$begin, PDO::PARAM_INT);
				$stmt->bindParam(":numbers", $numbers, PDO::PARAM_INT);
				$stmt->execute();

				return $stmt->fetchall(PDO::FETCH_ASSOC);
			}
			catch (PDOException $e) {
				echo "Problem : " . $e->getMessage() . " " . $e->getLine();
			}
		}


		public function rowByName($name) {
			try {

				$stmt = $this->conn->prepare("	select 		s.id,
															s.name,
															s.description,
															s.image,
															s.quantity_type,
															s.minimum_quantity,
															s.producer,
															s.price,
															s.promo,
															s.promo_price,
															c.name as category,
															s.front_page
													from 	Stock s
													join 	category c
														on 	c.id = s.category_id
													where 	s.name like :word ");

				$stmt->bindParam(':word', $name);
				$stmt->execute();
				$stock = $stmt->fetchall(PDO::FETCH_ASSOC);
				$temp = new Stock();
				if (count($stock)>0)
					$temp->Init($stock[0]);
				return $temp;
			}
			catch (PDOException $e) {
				echo "Problem : " . $e->getMessage() . " " . $e->getLine();
			}
		}



		public function setStocksNumber($stocks_number) {
			$this->stocks_number = $stocks_number;
		}


		public function getStocksNumber() {
			return $this->stocks_number;
		}
	}

?>